﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {




        if ((Session["username"]) != null)
        {
            lbl_najava.Text = vrati_ime_i_prezime((string)Session["username"] + "  ");
            mv_najava.ActiveViewIndex = 1;
            LinkButton3.Visible = true;
            LinkButton4.Visible = true;
        }
        else
        {
            mv_najava.ActiveViewIndex = 0;
            LinkButton3.Visible = false;
            LinkButton4.Visible = false;
        }
    }
    protected void btn_najava_Click(object sender, EventArgs e)
    {

        if (txt_user.Text == "")
        {
            lbl_result.Text = "Внесете го корисничкото име!";
        }
        else
            if (txt_pass.Text == "")
            {
                lbl_result.Text = "Внесете ја лозинката!";
            }
            else
            {

                SqlConnection konekcija = new SqlConnection();
                konekcija.ConnectionString = ConfigurationManager.ConnectionStrings["Konekcija"].ConnectionString;

                string que = "SELECT * FROM Photographers WHERE username=@username AND password=@password";

                SqlCommand com = new SqlCommand(que, konekcija);
                com.Parameters.AddWithValue("@username", txt_user.Text);
                com.Parameters.AddWithValue("@password", txt_pass.Text);
                SqlDataReader reader;

                try
                {
                    konekcija.Open();
                    reader = com.ExecuteReader();
                    while (reader.Read())
                    {
                        
                        Session["username"] = txt_user.Text;
                        lbl_najava.Text = vrati_ime_i_prezime(txt_user.Text)+"  ";
                        mv_najava.ActiveViewIndex = 1;
                        txt_user.Text = "";
                        txt_pass.Text = "";
                        LinkButton3.Visible = true;
                        LinkButton4.Visible = true;
                    }
                    reader.Close();
                }
                catch (Exception err)
                {
                    lbl_result.Text = "Error reading list of names. ";
                    lbl_result.Text += err.Message;
                }
                finally
                {
                    konekcija.Close();
                }
            }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registracija.aspx");
    }
    protected string vrati_ime_i_prezime(string username)
    {
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = @"Data Source=localhost\SQLEXPRESS;" + "Initial Catalog=Photographers;Integrated Security=SSPI";

        string sqlQuestion1 = "SELECT * FROM Photographers WHERE username=@username";

        SqlCommand komanda1 = new SqlCommand(sqlQuestion1, myConnection);
        komanda1.Parameters.AddWithValue("@username", username);
        SqlDataReader citac1;
        string ime_prezime = "";
        try
        {
            myConnection.Open();
            citac1 = komanda1.ExecuteReader();
            while (citac1.Read())
            {
                ime_prezime = citac1["first_name"].ToString() + " " + citac1["last_name"].ToString();
            }
        }
        catch (Exception err)
        {
            lbl_result.Text += "error ";
            lbl_result.Text += err.Message;
            myConnection.Close();
        }
        finally
        {
            myConnection.Close();
        }
        if (ime_prezime == "")
            return "unknown";
        else
            return ime_prezime;

    }
    protected void lnk_najava_Click(object sender, EventArgs e)
    {
        lbl_najava.Text = "";
        Session["username"] = null;
        mv_najava.ActiveViewIndex = 0;
        LinkButton4.Visible = false;
        LinkButton3.Visible = false;
    }

    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Fotografii.aspx");
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
            Response.Redirect("Profil.aspx");
    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
            Response.Redirect("Portfolio.aspx");
    }

    

    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        Response.Redirect("GlavnaStrana.aspx");
    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {

    }
}
